package czr.tetris.frame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import czr.tetris.beans.Block;
import czr.tetris.beans.Constant;

/**
 * 画方块
 * 
 * @author chenzhirong
 *
 */
public class TetrisPanel extends JPanel implements KeyListener{
	
	/**
	 * 序号
	 */
	private static final long serialVersionUID = 4001657124656214016L;
	
	/**
	 * 方块坐标
	 */
	Block rect[] = { new Block(4,0),
			         new Block(5,0),
			         new Block(6,0),
			         new Block(7,0)};
    /**
     * 方块构造方法
     */
	public TetrisPanel() {
		
	}
	
	public TetrisPanel(int x, int y) {

	}
	
	/**
	 * 记录方块移动方向标签
	 */
	int tab;

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawRect(10, 10, Constant.blockSize * 12 + 1, Constant.blockSize * 18 + 1);
		g.setColor(Color.red);
		g.fillRect(rect[0].x * Constant.blockSize + 11, rect[0].y * Constant.blockSize + 11, Constant.blockSize,Constant.blockSize);
		g.fillRect(rect[1].x * Constant.blockSize + 11, rect[1].y * Constant.blockSize + 11, Constant.blockSize,Constant.blockSize);
		g.fillRect(rect[2].x * Constant.blockSize + 11, rect[2].y * Constant.blockSize + 11, Constant.blockSize,Constant.blockSize);
		g.fillRect(rect[3].x * Constant.blockSize + 11, rect[3].y * Constant.blockSize + 11, Constant.blockSize,Constant.blockSize);
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		switch(e.getKeyCode())  
        {  
            case KeyEvent.VK_UP:  
            	for (int i = 0; i < 4; i++) {
				}
                //调用repaint()函数，重新绘制小球位置  
                this.repaint();  
                break;  
            case KeyEvent.VK_DOWN:  
            	tab = 1;
            	if(isBoundary("y")){
            	for (int i = 0; i < 4; i++) {
            			rect[i].y += 1;
            			repaint();
            		}
				}
                this.repaint();  
                break;  
            case KeyEvent.VK_LEFT:  
            	tab = -1;
            	if(isBoundary("x")){
            	for (int i = 0; i < 4; i++) {
            			rect[i].x -= 1;
            			repaint();
            		}
				}
                this.repaint();  
                break;   
            case KeyEvent.VK_RIGHT: 
            	tab = 1;
            	if(isBoundary("x")){
            	for (int i = 0; i < 4; i++) {
            			rect[i].x += 1;
            			repaint();
            		}
				}
                this.repaint();  
                break;  
        }  
	}
	
	/**
	 * 判断方块是否出界超出范围
	 */
	private boolean isBoundary(String isXY) {
		for (int i = 0; i < 4; i++) {
			if("x".equals(isXY)){
				if(rect[i].x + tab < 0 || rect[i].x + tab > 11){
					return false;
				}
			}
			if("y".equals(isXY)){
				if(rect[i].y + tab < 0 || rect[i].y + tab > 17){
					return false;
				}
			}
		}
		return true;
	}
	
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}
